package org.edu.modules.oa.config;


import org.edu.modules.oa.vo.QueryCondition;

import java.util.List;


public class QueryConditionTool {
    private static QueryConditionTool queryConditionTool;
    private static   String conditionStr="";

    public static QueryConditionTool getQueryConditionTool() {
        if (queryConditionTool == null) {
            queryConditionTool = new QueryConditionTool();
        }
        return queryConditionTool;
    }


    public String advancedQuery ( List<QueryCondition> list) {




        String queryCondition="";
        for (QueryCondition query:list){
            if (query.getRule().equals("or")) {
                queryCondition=" or ";
                break;
            }
            if (query.getRule().equals("and")) {
                queryCondition=" and ";
                break;

            }
        }


//         条件的字符串
         conditionStr = "where 1=1";
        for (QueryCondition query : list) {
            if (query.getRule().equals("eq")) {
                conditionStr += queryCondition + query.getField() + " = '" + query.getVal() + "' ";
            }
            if (query.getRule().equals("like")) {
                conditionStr += queryCondition + query.getField() + " like '%" + query.getVal() + "%' ";
            }
            if (query.getRule().equals("right_like")) {
                conditionStr += queryCondition + query.getField() + " like '" + query.getVal() + "%' ";
            }
            if (query.getRule().equals("left_like")) {

                conditionStr += queryCondition + query.getField() + " like '%" + query.getVal() + "' ";
            }
            if (query.getRule().equals("in")) {

                conditionStr += queryCondition + query.getField() + " in '(" + query.getVal() + ")' ";
            }
            if (query.getRule().equals("ne")) {

                conditionStr += queryCondition + query.getField() + " != '" + query.getVal() + "' ";
            }
            if (query.getRule().equals("gt")) {

                conditionStr += queryCondition + query.getField() + " &gt; '" + query.getVal() + "' ";
            }
            if (query.getRule().equals("ge")) {

                conditionStr += queryCondition + query.getField() + " &gt;= '" + query.getVal() + "' ";
            }
            if (query.getRule().equals("lt")) {

                conditionStr += queryCondition + query.getField() + " &lt; '" + query.getVal() + "' ";
            }
            if (query.getRule().equals("le")) {

                conditionStr += queryCondition + query.getField() + " &lt;= '" + query.getVal() + "' ";
            }


        }
        for (QueryCondition query:list){
            if (query.getRule()!=null&&query.getRule().equals("carId")) {
                conditionStr += "and car_id = " + query.getVal() + " ";
                break;
            }

        }
        // 拼接一下车辆ID
        return conditionStr;
    }
}
